<template>
  <div class="info">
    <template>
      <el-skeleton :loading="loading"/>
    </template>
    <template v-if="!loading">
      <el-form :model="searchInfo" inline>
        <el-form-item label="用户名">
          <span class="name" slot="label">用户名</span>
          <el-input clearable v-model="searchInfo.account" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <span class="name" slot="label">姓名</span>
          <el-input clearable v-model="searchInfo.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primery" icon="el-icon-search" @click="getList()">查询</el-button>
          <el-button icon="el-icon-refresh-right" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
      <el-form>
        <el-form-item>
          <el-button type="success" icon="el-icon-folder-add" @click="openWin()">添加</el-button>
          <el-button type="info" icon="el-icon-thumb" @click="setRole">角色分配</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="userList" border highlight-current-row @current-change="selectRow">
        <el-table-column label="编号" prop="id"/>
        <el-table-column label="用户名" prop="account"/>
        <el-table-column label="姓名" prop="name"/>
        <el-table-column label="性别" prop="sexName"/>
        <el-table-column label="角色" prop="roleName"/>
        <el-table-column label="部门" prop="deptName"/>
        <el-table-column label="生日" prop="birthday" show-overflow-tooltip/>
        <el-table-column label="手机号" prop="phone"/>
        <el-table-column label="邮箱" prop="email" show-overflow-tooltip/>
        <el-table-column label="状态" prop="statusName"/>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <el-button type="success" size="small" @click="openWin(scope.row)">修改</el-button>
            <el-button type="danger" size="small" @click="delInfo(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination layout="total,prev,pager,next,sizes,jumper"
                     :total="pageInfo.total"
                     :page-size="pageInfo.size"
                     :current-page="pageInfo.current"
                     :page-sizes="[5,10,20,30,50,100]"
                     @size-change="sizeChange"
                     @current-change="currentChange"
      />
    </template>
    <UserEdit ref="userEdit" @reloadTable="getList"/>
    <UserRole ref="userRole" @reloadTable="getList"/>
  </div>
</template>

<script>
import UserEdit from '@/views/user/UserEdit'
import UserRole from "@/views/user/UserRole";

export default {
  name: 'User',
  data() {
    return {
      loading: true,
      searchInfo: {
        account: '',
        name: '',
        page: 1,
        limit: 5
      },
      userList: [],
      pageInfo: {},
      selectRowDate: {},
    }
  },

  methods: {
    getList() {
      this.$http.get('user/list', this.searchInfo).then(({data}) => {
        this.loading = false
        if (data.code === 20000) {
          this.userList = data.data.records
          this.pageInfo = data.data
        }
      })
    },
    sizeChange(limit) {
      this.searchInfo.limit = limit
      this.getList()
    },
    currentChange(page) {
      this.searchInfo.page = page
      this.getList()
    },
    openWin(info) {
      this.$refs.userEdit.init(info)
    },
    delInfo(userId) {
      this.$confirm('确定删除该数据吗？', '询问').then(() => {
        this.$axios.delete('user', {params: {userId}}).then(({data}) => {
          if (data.code === 20000) {
            this.$message({
              message: '删除成功',
              type: 'success',
              onClose: () => {
                this.getList()
              }
            })
          } else {
            this.$alert('删除失败！', {type: 'error'})
          }
        })
      })
    },
    selectRow(row) {
      this.selectRowDate = row
    },
    setRole() {
      if (this.selectRowDate.id) {
        this.$refs.userRole.init(this.selectRowDate)
      } else {
        this.$alert('请先选择一个用户', {type: 'error'})
      }
    },
    resetForm() {
      this.searchInfo.account = '';
      this.searchInfo.name = '';
      this.getList()
    }
  },
  created() {
    this.$nextTick(() => {
      this.getList()
    })
  },
  components: {UserRole, UserEdit},
}
</script>

<style scoped>

</style>
